package logic

import (
	"cart/mysql"
	"context"

	"cart/cart"
	"cart/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *cart.GoodsAddRequest) (*cart.GoodsAddResponse, error) {
	// todo: 商品添加
	var err error
	var g mysql.Goods
	err = g.Find(in.Name)
	if err != nil {
		logx.Error("此商品已经发布", err)
		return nil, err
	}

	g.Name = in.Name
	g.Price = float64(in.Price)
	g.Stock = int(in.Num)

	err = g.Create()
	if err != nil {
		logx.Error("发布失败", err)
		return nil, err
	}

	return &cart.GoodsAddResponse{
		Id: int64(g.ID),
	}, nil
}
